package com.example.demo.redis.client;

import com.example.demo.redis.able.CacheInterface;
import com.example.demo.redis.util.RedissonUtils;
import lombok.extern.slf4j.Slf4j;

import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.codec.TypedJsonJacksonCodec;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
public class RedissonBucketClient {

    @Autowired
    private RedissonClient redissonClient;

/**
     * 获取Bucket
     */

    public <V> RBucket<V> getBucket(CacheInterface cacheInterface, Codec codec) {
        String cacheKey = RedissonUtils.getCacheKey(cacheInterface, null);
        return redissonClient.getBucket(cacheKey, codec);
    }

/**
     * 获取Bucket
     */

    public <V> RBucket<V> getBucket(CacheInterface cacheInterface, Class<V> valueClass) {
        return getBucket(cacheInterface, null, valueClass);
    }


/**
     * 获取Bucket
     */

    public <V> RBucket<V> getBucket(CacheInterface cacheInterface, String key, Class<V> valueClass) {
        String cacheKey = RedissonUtils.getCacheKey(cacheInterface, key);
        Codec codec = new TypedJsonJacksonCodec(valueClass);
        return redissonClient.getBucket(cacheKey, codec);
    }


/**
     * 获取Bucket并赋值
     */

    public <V> RBucket<V> getAndSet(CacheInterface cacheInterface, V value, Class<V> valueClass) {
        return getAndSet(cacheInterface, null, value, valueClass);
    }


/**
     * 获取Bucket并赋值
     */

    public <V> RBucket<V> getAndSet(CacheInterface cacheInterface, String key, V value, Class<V> valueClass) {
        RBucket<V> bucket = getBucket(cacheInterface, key, valueClass);
        bucket.set(value);
        return bucket;
    }


/**
     * 获取Bucket并赋值设置过期时间
     */

    public <V> RBucket<V> setAndExpire(CacheInterface cacheInterface, V value, Class<V> valueClass) {
        return setAndExpire(cacheInterface, null, value, valueClass);
    }


/**
     * 获取Bucket并赋值设置过期时间
     */

    public <V> RBucket<V> setAndExpire(CacheInterface cacheInterface, String key, V value, Class<V> valueClass) {
        RBucket<V> bucket = getBucket(cacheInterface, key, valueClass);
        bucket.set(value, cacheInterface.getExpire(), cacheInterface.getUnit());
        return bucket;
    }


/**
     * 设置过期时间
     */

    public <V> boolean expire(CacheInterface cacheInterface, Class<V> valueClass) {
        return expire(cacheInterface, null, valueClass);
    }


/**
     * 设置过期时间
     */

    public <V> boolean expire(CacheInterface cacheInterface, String key, Class<V> valueClass) {
        RBucket<V> bucket = getBucket(cacheInterface, key, valueClass);
        return bucket.expire(cacheInterface.getExpire(), cacheInterface.getUnit());
    }


/**
     * 删除缓存
     */

    public <V> boolean delete(CacheInterface cacheInterface, Class<V> valueClass) {
        return delete(cacheInterface, null, valueClass);
    }


/**
     * 删除缓存
     */

    public <V> boolean delete(CacheInterface cacheInterface, String key, Class<V> valueClass) {
        RBucket<V> bucket = getBucket(cacheInterface, key, valueClass);
        return bucket.delete();
    }


}

